package com.cq.base.mvp

import android.content.Context
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.cq.base.dialog.LoadingDialog


/**
 * 作者: CQ
 * 日期: 2021-03-23
 * 说明: MVP基类activity
 */
abstract class BaseActivity<P : BasePresenter<*>> : AppCompatActivity(), BaseIView {

    protected lateinit var presenter: P
    protected lateinit var context: Context
    protected lateinit var loadingDialog: LoadingDialog

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(getLayoutId())

        context = this

        loadingDialog =
            LoadingDialog.Builder(context).setMessage("加载中...").setCancelable(true).setCancelOutside(false).create()

        this.presenter = createPresenter()
        presenter.attach(this)
        presenter.onCreate()

    }

    override fun onResume() {
        super.onResume()
        presenter.onResume()
    }

    override fun onDestroy() {
        super.onDestroy()
        presenter.onDestroy()
    }

    override fun showToast(toast: String?) {
        Toast.makeText(this, toast, Toast.LENGTH_SHORT).show()
    }

    override fun showLoadingView() {
        loadingDialog.show()
    }

    override fun endLoadingView() {
        loadingDialog.dismiss()
    }


    /**
     * 不同activity的布局接口
     * @return
     */
    abstract fun getLayoutId(): Int

    /**
     * 绑定的Presenter
     */
    abstract fun createPresenter(): P
}